using System.Xml.Serialization;

namespace HengTong.Kingdee.WSWMSImportBillFacadeSrvProxyService
{
    /// <summary>
    /// 金蝶数据通用扩展方法
    /// </summary>
    public static class KingdeeDataExtensions
    {
        /// <summary>
        /// 将 exportDataResponse 转换为强类型的数据列表（泛型方法）
        /// </summary>
        /// <typeparam name="T">目标数据类型，必须是 DataInfo 根节点的类型</typeparam>
        /// <param name="response">exportDataResponse 响应对象</param>
        /// <returns>强类型数据列表</returns>
        public static List<T> ToDataList<T>(this exportDataResponse response) where T : class
        {
            var result = new List<T>();
            if (response?.exportDataReturn == null)
            {
                return result;
            }

            var serializer = new XmlSerializer(typeof(T));
            foreach (var item in response.exportDataReturn)
            {
                // object[] 中的每个元素实际上是 string 类型的 XML
                var xmlString = item?.ToString();
                if (string.IsNullOrWhiteSpace(xmlString))
                {
                    continue;
                }

                try
                {
                    using var reader = new StringReader(xmlString);
                    var dataInfo = serializer.Deserialize(reader) as T;
                    if (dataInfo != null)
                    {
                        result.Add(dataInfo);
                    }
                }
                catch (Exception ex)
                {
                    // 可以记录日志或抛出异常，根据需要处理
                    throw new InvalidOperationException($"解析金蝶数据失败: {ex.Message}", ex);
                }
            }

            return result;
        }

        /// <summary>
        /// 将单个 XML 字符串转换为强类型对象（泛型方法）
        /// </summary>
        /// <typeparam name="T">目标数据类型，必须是 DataInfo 根节点的类型</typeparam>
        /// <param name="xmlString">XML 字符串</param>
        /// <returns>强类型对象</returns>
        public static T ToData<T>(this string xmlString) where T : class
        {
            if (string.IsNullOrWhiteSpace(xmlString))
            {
                return null;
            }

            var serializer = new XmlSerializer(typeof(T));
            using var reader = new StringReader(xmlString);
            return serializer.Deserialize(reader) as T;
        }

        /// <summary>
        /// 将 exportDataResponse 转换为 MeasureUnit 数据列表（便捷方法）
        /// </summary>
        public static List<MeasureUnitDataInfo> ToMeasureUnitDataList(this exportDataResponse response)
        {
            return response.ToDataList<MeasureUnitDataInfo>();
        }

        /// <summary>
        /// 将单个 XML 字符串转换为 MeasureUnit 对象（便捷方法）
        /// </summary>
        public static MeasureUnitDataInfo ToMeasureUnitData(this string xmlString)
        {
            return xmlString.ToData<MeasureUnitDataInfo>();
        }

        /// <summary>
        /// 将 exportDataResponse 转换为 MeasureUnitGroup 数据列表（便捷方法）
        /// </summary>
        public static List<MeasureUnitGroupDataInfo> ToMeasureUnitGroupDataList(this exportDataResponse response)
        {
            return response.ToDataList<MeasureUnitGroupDataInfo>();
        }

        /// <summary>
        /// 将单个 XML 字符串转换为 MeasureUnitGroup 对象（便捷方法）
        /// </summary>
        public static MeasureUnitGroupDataInfo ToMeasureUnitGroupData(this string xmlString)
        {
            return xmlString.ToData<MeasureUnitGroupDataInfo>();
        }

        /// <summary>
        /// 将 exportDataResponse 转换为 EasCompany 数据列表（便捷方法）
        /// </summary>
        public static List<EasCompanyDataInfo> ToEasCompanyDataList(this exportDataResponse response)
        {
            return response.ToDataList<EasCompanyDataInfo>();
        }

        /// <summary>
        /// 将单个 XML 字符串转换为 EasCompany 对象（便捷方法）
        /// </summary>
        public static EasCompanyDataInfo ToEasCompanyData(this string xmlString)
        {
            return xmlString.ToData<EasCompanyDataInfo>();
        }

        /// <summary>
        /// 将 exportDataResponse 转换为 EasWarehouse 数据列表（便捷方法）
        /// </summary>
        public static List<EasWarehouseDataInfo> ToEasWarehouseDataList(this exportDataResponse response)
        {
            return response.ToDataList<EasWarehouseDataInfo>();
        }

        /// <summary>
        /// 将单个 XML 字符串转换为 EasWarehouse 对象（便捷方法）
        /// </summary>
        public static EasWarehouseDataInfo ToEasWarehouseData(this string xmlString)
        {
            return xmlString.ToData<EasWarehouseDataInfo>();
        }

        /// <summary>
        /// 将 exportDataResponse 转换为 EasLocation 数据列表（便捷方法）
        /// </summary>
        public static List<EasLocationDataInfo> ToEasLocationDataList(this exportDataResponse response)
        {
            return response.ToDataList<EasLocationDataInfo>();
        }

        /// <summary>
        /// 将单个 XML 字符串转换为 EasLocation 对象（便捷方法）
        /// </summary>
        public static EasLocationDataInfo ToEasLocationData(this string xmlString)
        {
            return xmlString.ToData<EasLocationDataInfo>();
        }

        /// <summary>
        /// 将 exportDataResponse 转换为 EasInventory 数据列表（便捷方法）
        /// </summary>
        public static List<EasInventoryDataInfo> ToEasInventoryDataList(this exportDataResponse response)
        {
            return response.ToDataList<EasInventoryDataInfo>();
        }

        /// <summary>
        /// 将单个 XML 字符串转换为 EasInventory 对象（便捷方法）
        /// </summary>
        public static EasInventoryDataInfo ToEasInventoryData(this string xmlString)
        {
            return xmlString.ToData<EasInventoryDataInfo>();
        }
    }
}
